<#--<#include "../layout/base.ftl">-->
<#--<#macro header>-->
<script type="text/javascript">
var companyList = [
<#if companyList??&&companyList?size&gt;0>
    {text: "", value: "0"},
    <#list companyList as company>
        {text: "${company[2]!""}", value: "${company[0]!""}"}<#if company_has_next>,</#if>
    </#list>
</#if>
];

$(function () {
    $("#ca_search_panel").omPanel({
        title: "高级搜索"
    });

    $("#ca_company_name").omCombo({
        width: 100,
        dataSource: companyList,
        listAutoWidth: true,
        filterStrategy: 'anywhere',
        editable: false,
        value: '0'
    });


    $("#ca_status").omCombo({
        width: 100,
        dataSource: [
            {text: '未导入', value: '1'},
            {text: '导入久其成功', value: '7'},
            {text: '从久其删除成功', value: '9'},
            {text: '导入久其失败', value: '6'},
            {text: '从久其删除失败', value: '8'},
            {text: '导入U8成功', value: '3'},
            {text: '从U8删除成功', value: '5'},
            {text: '导入U8失败', value: '2'},
            {text: '从U8删除失败', value: '4'}
        ],
        value: '1',
        editable: false
    });


    $('span#ca_button_search').omButton({
        width: 70,
        icons: {left: '${base}/css/default/images/advanced/search.png'},
        onClick: function (event) {
            $('#ca_grid').omGrid({
                extraData: {companyId: $("#ca_company_name").omCombo('value'),
                    voucherId: $("#ca_voucher_id").val(),
                    status: $("#ca_status").omCombo('value'),
                    operator: $("#ca_operator").val()}
            });
        }
    });

    var grid = $('#ca_grid').omGrid({
        method: 'POST',
        dataSource: "${base}/cash_accept/grid_json",
        extraData: {companyId: $("#ca_company_name").omCombo('value'),
            voucherId: $("#ca_voucher_id").val(),
            status: $("#ca_status").omCombo('value'),
            operator: $("#ca_operator").val()},
        height: 380,
        width: 'fit',
        autoFit: true,
        limit: 10,
        colModel: [
            {header: '公司名称', name: 'company', align: 'left'},
            {header: '单据号', name: 'no', align: 'left'},
            {header: '凭证类型', name: 'typeName', align: 'left'},
            {header: '票据类型', name: 'billType', align: 'left'},
            {header: '票据号码', name: 'billNo', align: 'left'},
            {header: '票面金额', name: 'billMoney', align: 'left'},
            {header: '制单人', name: 'operator', align: 'left'},
            {header: '制单日期', name: 'time', align: 'left'},
            {header: '凭证号', name: 'voucher', align: 'left'},
            {header: '操作', name: 'operation', width: 120, align: 'center', renderer: function (colValue, rowData, rowIndex) {
                if (colValue == "add") {
                    var button = '<button onClick="caShowDialog(' + rowData.id + ')">导入</button>';
                    if (rowData.type == 2)
                        button += '<button onClick="caRevokeNote(' + rowData.id + ',1)">取消贴现托收</button>';
                    if (rowData.type == 3)
                        button += '<button onClick="caRevokeNote(' + rowData.id + ',2)">取消票据承兑</button>';
                    return button;
                }
                if (colValue == "delete")
                    return '<button onClick="caRevokeVoucher(' + rowData.id + ')">删除凭证</button>';
                if (colValue == "reset")
                    return '<button onClick="caRestVoucher(' + rowData.id + ')">重置</button>';
                return '';
            }}
        ]
    });

    $(window).scroll(function () {
        $("#ca_grid").omGrid('resize');
    });

    $("#ca_dialog").omDialog({
        title: "导入凭证",
        autoOpen: false,
        resizable: true,
        modal: true,
        width: 800,
        buttons: [
            {
                text: "确定",
                click: function () {
                    var rows = $("#ca_items tr");
                    var items = "[";
                    for (var i = 0; i < rows.length; i++) {
                        var inputs = $(rows[i]).find(":input");
                        items += "{";
                        for (var j = 0; j < inputs.length; j++) {
                            items += "\"" + $(inputs[j]).attr("name") + "\":\"" + $(inputs[j]).val() + "\",";
                        }
                        items = items.substring(0, items.length - 1) + "},";
                    }
                    items = items.substring(0, items.length - 1) + "]";
                    $.ajax({
                        type: "POST",
                        url: '${base}/cash_accept/generate_voucher_json',
                        data: {'id': $("#ca_id").val(), 'attachment': $("#ca_attachment").val(),
                            importType: $("#ca_importType").omCombo("value"), voucherNum: $("#ca_voucherNumber").val(),
                            voucherItems: items},
                        dataType: 'json',
                        cache: false,
                        beforeSend: function () {
                            $("#ca_dialog").omDialog("close");
                            $.blockUI({ message: '<h2>请稍后...</h2>' });
                        },
                        success: function (data) {
                            $.unblockUI();
                            $('span#ca_button_search').omButton("click");
                            var type = data.responseflag == "1" ? "success" : "error";
                            $.omMessageBox.alert({
                                type: type,
                                title: '提示',
                                content: data.responsemessage
                            });
                        }
                    });
                }
            },
            {
                text: "取消",
                click: function () {
                    $("#ca_dialog").omDialog("close");
                }
            }
        ]
    });
});

var ca_item_table_row = '<tr><td style="padding: 0px">' +
        '<input type="text" style="width:98%" name="digest" value=":digest"/></td>' +
        '<td align="left">:subjectname</td>' +
        '<td width="10%" align="right">:debit</td>' +
        '<td width="10%" align="right">:credit</td>' +
        '<td width="10%">:departmentname</td>' +
        '<td width="10%">:product</td></tr>';
function caShowDialog(id) {
    var data = {"id": id};
    $("#ca_id").val(id);
    $.post("${base}/cash_accept/voucher_json", data, function (data, textStatus, jqXHR) {
        if (data.responseflag == "0") {
            $.omMessageBox.alert({
                type: "error",
                title: '提示',
                content: data.responsemessage
            });
            return;
        }
        $("#ca_vchrtypecode").html(data.voucher.master.vchrtypecode);
        $("#ca_createuser").html(data.voucher.master.createuser);
        $("#ca_createdate").val(data.voucher.master.systemdate);
        $("#ca_createdate").omCalendar({
            editable: false
        });
        $("#ca_items").html("");
        if (data.voucher.items.length != 0) {
            var items_table = "";
            for (var i = 0; i < data.voucher.items.length; i++) {
                var item = data.voucher.items[i];
                var temp = ca_item_table_row.replace(":digest", item.digest);
                temp = temp.replace(":subjectname", item.subjectname);
                temp = temp.replace(":debit", item.debit);
                temp = temp.replace(":credit", item.credit);

                if (item.isdept == 0)
                    temp = temp.replace(":departmentname", "");
                else
                    temp = temp.replace(":departmentname", '<select style="width:100px" name="departmentname"><option></option></select>');

                if (item.isproc == 0)
                    temp = temp.replace(":product", "");
                else
                    temp = temp.replace(":product", '');

                items_table += temp;
            }
            $("#ca_items").html(items_table);
        }
        $("#ca_importType").omCombo({
            width: 100,
            dataSource: [
                {text: '生成凭证号', value: '1'},
                {text: '指定凭证号', value: '2'}
            ],
            value: '1',
            editable: false
        });
        $("#ca_dialog").omDialog('open');
    }, "json");
}

function caRevokeNote(id, type) {
    var data = {
        "id": id,//单据号
        "revokeType": type //1=合并，2=删除
    };
    $.omMessageBox.confirm({
        title: '提示',
        content: '是否确认取消？',
        onClose: function (v) {
            if (v) {
                $.post('${base}/cash_accept/revoke_note', data,
                        function (data) {
                            if (data.success == "1")
                                $('span#ca_button_search').omButton("click");
                            var type = data.success == "1" ? "success" : "error";
                            $.omMessageBox.alert({
                                type: type,
                                title: '提示',
                                content: data.message
                            });
                        }, "json");
            }
        }
    });
}

function caRestVoucher(id) {
    $.omMessageBox.confirm({
        title: '提示',
        content: '是否确认重置该凭证？',
        onClose: function (v) {
            if (v) {
                var data = {"id": id};
                $.post("${base}/cash_accept/rest_voucher_json", data,
                        function (data, textStatus, jqXHR) {
                            if (data.responseflag == "1") {
                                $('span#ca_button_search').omButton("click");
                            }
                            var type = data.responseflag == "1" ? "success" : "error";
                            $.omMessageBox.alert({
                                type: type,
                                title: '提示',
                                content: data.responsemessage
                            });
                        }, "json");
            }
        }
    });
}
</script>
<#--</#macro>-->
<#--<#macro body>-->
<div id="ca_search_panel" class="search_panel">
    <div>
        <span class="label">公&nbsp;&nbsp;&nbsp;司：</span>
        <input id="ca_company_name" type="text"/>
        <span class="label">状&nbsp;&nbsp;&nbsp;态：</span>
        <input id="ca_status"/>
        <span>凭证号：</span>
        <input id="ca_voucher_id" type="text" class="input-text"/>
        <span>操作人：</span>
        <input id="ca_operator" type="text" class="input-text"/>
        <span id="ca_button_search">搜索</span>
    </div>
</div>
<br/>
<table id="ca_grid"></table>

<div id="ca_dialog">
    <form action='' method='post'>
        <input id="ca_id" type="hidden"/>
        <table width="100%">
            <tr>
                <td width="70px">字号</td>
                <td><span id="ca_vchrtypecode" style="font-size: 12px;font-weight: bold"></span></td>
                <td width="70px">制单人</td>
                <td id="ca_createuser">
                </td>
                <td width="70px">制单日期</td>
                <td>
                    <input id="ca_createdate" type="text" style="width: 120px"/>
                </td>
                <td width="70px">附单据数</td>
                <td>
                    <input id="ca_attachment" type="text" style="width: 120px"/>
                </td>
            </tr>
        </table>
        <br/>
        <table width="100%" class="form outtable">
            <thead>
            <tr>
                <th>摘要</th>
                <th>科目</th>
                <th>借方</th>
                <th>贷方</th>
                <th>部门</th>
                <th>项目</th>
            </tr>
            </thead>
            <tbody id="ca_items">
            </tbody>
        </table>
        <table width="100%">
            <tr>
                <td width="70px">导入方式</td>
                <td><input id="ca_importType"/></td>
                <td width="70px">凭证号</td>
                <td><input id="ca_voucherNumber" type="text" style="width: 118px"/></td>
            </tr>
        </table>
    </form>
</div>
<#--</#macro>-->